<template>
	<view class="content">
		<view class="loginView">
			<view class="loginTitle">仅限购买了 RPCC 服务的用户使用授权</view>
			<button class="loginBtn cu-btn round lg" @tap="toLogin">微  信  授  权</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				setViewHeight: '',
				setButtonHeight: ''
				
			}
		},
		mounted () {
			// 获取屏幕高度
			// 注意，这里要用个变量存this，不然进到getSystemInfo后this指向会变化，找不到data变量
			var _this = this
			uni.getSystemInfo({
			  success: function (res) {
				_this.setButtonHeight = 'bottom:' + (res.windowWidth*0.2) + 'px'
				_this.setViewHeight = 'margin-top:' + res.windowWidth + 'px'
			  }
			})
		},
		methods: {
			// 获取授权详情
			toLogin() {
				var _this = this
				// 无openId
				uni.getUserProfile({  // 微信授权弹窗(2.27.0以下版本才弹出)
					desc: "授权",
					success(agree) {
						// 展示加载框
						uni.showLoading({
							title: '加载中...',
						})
						console.log('同意授权的回调', agree)
						uni.login({	// 登录并获取code(用户获取微信用户的openId)
						    provider: 'weixin2', //使用微信登录
						    success: function (loginRes) {
								if (loginRes.errMsg === 'login:ok') {
									console.log('登录微信成功的回调', loginRes)
									uniCloud.callFunction({
										name:'get_or_create',
										data:{
											code:loginRes.code,
											wxName:agree.userInfo.nickName
										},
										success(openIdRes) {
											console.log('获取并创建openId成功', openIdRes)
											if (openIdRes.result.code === 200) {
												uni.setStorage({
													key:'openId',
													data:openIdRes.result.data.open_id,
													success() {
														uni.reLaunch({
															url: '/pages/home/home'
														});
													},
													fail(storageErr) {
														uni.showToast({ icon: 'none', title: `openId缓存失败: ${storageErr.errMsg}` })
													}
												});
											} else {
												uni.showToast({ icon: 'none', title: `${openIdRes.result.msg}` })
											}
										},
										fail(openIdErr) {
											console.log('获取并创建openId失败', openIdErr)
											uni.showToast({ icon: 'none', title: `获取并创建openId失败: ${openIdErr.errMsg}` })
										}
									})
								} else {
									console.log('登录微信失败的回调', loginRes)
									uni.showToast({ icon: 'none', title: `login失败: ${loginRes.errMsg}` })
								}
							},fail(loginErr) {
								console.log('登录微信失败的回调', loginErr)
								uni.showToast({ icon: 'none', title: `login失败: ${loginErr.errMsg}` })
							}
					    })
					}, fail(refuse) {
						console.log('拒绝授权的回调', refuse)
						uni.showToast({ icon: 'none', title: `授权已拒绝` })
					}, complete: () => {
						// 隐藏loadings
						setTimeout(function () {
							uni.hideLoading();
						}, 2000);
				    }
				})
			}
		}
	}
</script>

<style>
	.content {
		/* background-image: url('/static/logo.png'); */
		background-image: url('');
		width: 100%;
		height: 100vh;
		background-position: center;
		background-size: 100% 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		
	}
	.logo {
		width: 100%;
	}
	.loginBtn{
	    background: linear-gradient(to right, #4bb0ff, #3EDF83);
		color: white;
	}
	.loginTitle {
		margin-bottom: 5px;
		font-size: 14px;
	}
	
	.loginView {
		width: 35%;
		text-align: center;
		color: #e6e6e6;
		margin-left: 55%;
		margin-top: 100%;
	}
</style>